package com.share.wechat.core.repository.dao;

/**
 * Created by er on 2017/12/15.
 */

import com.share.wechat.page.constant.PageConstant;
import com.share.wechat.page.help.PageModel;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * 商品复杂的查询接口
 */
@Repository
public class DetailQueryDao {
    @Resource
    private EntityManager entityManager;
    private final Logger logger = Logger.getLogger(this.getClass());

    /**
     * 根据用户信息查
     * 查询出当前系统最受欢迎
     * 并且没有尝试订单的商品
     *
     * @param userInfo userInfo
     * @return 商品信息id集合
     */
    public List<String> findRecommendDetails(String userInfo, int page, int size) {
        if (page < 0) {
            page = PageConstant.DEFAULT_INT_PAGE;
        }
        if (size < 0) {
            size = PageConstant.DEFAULT_INT_SIZE;
        }
        if (StringUtils.isEmpty(userInfo)) {
            logger.info("用户信息未知" + userInfo);
            logger.info("查询不到数据 请传入正确的用户信息");
            return null;
        }

        PageModel<String> model = new PageModel<>(page, size);
        String sbSql = "SELECT DETAIL_ID FROM WEB_PRD_GROOM WHERE DETAIL_ID NOT IN (SELECT DETAIL_ID FROM WEB_USER_ORDER_REL WHERE USER_INFO=:userInfo)";
        Query query = entityManager.createNativeQuery(sbSql);
        query.setParameter("userInfo", userInfo);
        query.setFirstResult(model.getStartSum());
        query.setMaxResults(model.getEndSum());
        List resultList = query.getResultList();
         logger.debug("-----------------------------" + resultList + "-------------------------");

        return resultList;
    }


}
